{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\myogo\Princeton Dropbox\Motohiro Yogo\Koijen Yogo - International\JPE\Replication\Code\1 Data\Data3.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}31 Oct 2025, 09:19:17
{txt}
{com}. /* Define local and global variables */
> 
> local directory = "../../Data/IMF/CPIS";
{txt}
{com}. do global;
{txt}
{com}. /* global.do (STATA)
>         Define global variables in "Data".
>         by Ralph Koijen & Motohiro Yogo */
. 
. #delimit ;
{txt}delimiter now ;
{com}. clear all;
{res}{txt}
{com}. global year_min = 2003;
{txt}
{com}. global year_max = 2020;
{txt}
{com}. global small = 5e-4;
{txt}
{com}.         /* Minimum reportable threshold for CPIS */

{txt}end of do-file

{com}. /* Step 1: Restate IMF CPIS data by nationality */
> 
> /* Load data */
> 
> u IMF_CPIS if country!="USA" & !Iofc, clear;
{txt}
{com}.         /* Exclude offshore financial centers */
> 
> drop Iofc;
{txt}
{com}. /* Merge restatement matrices for bilateral positions */
> 
> joinby year country Counterpart type using Restatement_bilateral, unmatched(master);
{txt}
{com}. /* Assume nationality is same as residency if unmatched (including fund shares) */
> 
> replace counterpart = Counterpart if _merge==1;
{txt}(13,260 real changes made)

{com}. replace Value = 1 if _merge==1;
{txt}(13,260 real changes made)

{com}. drop _merge;
{txt}
{com}. /* Merge country code */
> 
> merge m:1 counterpart using Countries,
>         keepusing(Yeuro Ynat)
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}          15,589
{txt}{col 9}from master{col 30}{res}          15,589{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}       1,328,270{txt}  
{col 5}{hline 41}

{com}. /* Restate from residency to nationality */
> 
> replace amount = amount*Value;
{txt}(1,297,967 real changes made)

{com}. drop Value;
{txt}
{com}. /* Assume currency is same as nationality as starting point */
> 
> gen currency = counterpart;
{txt}
{com}. replace currency = "EUR" if year>=Yeuro;
{txt}(397,302 real changes made)

{com}. /* Merge IMF CPIS currency composition */
> 
> merge m:1 year country type currency using IMF_CPIS_currency,
>         nogen keep(master match);
{res}{txt}(label {bf:{txt}type_label} already defined)

{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}       1,006,510
{txt}{col 9}from master{col 30}{res}       1,006,510{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}         337,349{txt}  
{col 5}{hline 41}

{com}. /* Scale down to upper bound in domestic currency */
> 
> egen Tcounterpart = total(amount), missing by(year country type currency);
{txt}
{com}. expand 2 if Tcurrency<Tcounterpart, gen(duplicate);
{txt}(158,075 observations created)

{com}. replace amount = amount*Tcurrency/Tcounterpart if Tcurrency<Tcounterpart & !duplicate;
{txt}(158,075 real changes made)

{com}. /* Remainder in other currencies */
> 
> replace currency = "_OC" if Tcurrency<Tcounterpart & duplicate;
{txt}(158,075 real changes made)

{com}. replace amount = amount*(1-Tcurrency/Tcounterpart) if Tcurrency<Tcounterpart & duplicate;
{txt}(158,075 real changes made)

{com}. drop Tcurrency Tcounterpart duplicate;
{txt}
{com}. /* Construct dummy for domestic currency */
> 
> gen byte Idomestic = currency==counterpart | (currency=="EUR" & year>=Yeuro);
{txt}
{com}. drop currency Yeuro;
{txt}
{com}. /* Assign countries outside sample to other countries */
> 
> replace counterpart = "_OC" if year<Ynat;
{txt}(329,783 real changes made)

{com}. drop Ynat;
{txt}
{com}. /* Aggregate by nationality */
> 
> collapse (sum) amount, by(year country counterpart type Idomestic) fast;
{res}{txt}
{com}. /* Save data */
> 
> tempfile IMF_CPIS;
{txt}
{com}. save `IMF_CPIS';
{txt}{p 0 4 2}
file {bf}
C:\Users\myogo\AppData\Local\Temp\ST_9840_000001.tmp{rm}
saved
as .dta format
{p_end}

{com}. /* Step 2: Restate US Treasury data by nationality */
> 
> /* Load data */
> 
> u Treasury, clear;
{txt}
{com}. /* Merge restatement matrices for bilateral positions */
> 
> joinby year country Counterpart type using Restatement_bilateral, unmatched(master);
{txt}
{com}. /* Assume nationality is same as residency if unmatched (including fund shares) */
> 
> replace counterpart = Counterpart if _merge==1;
{txt}(1,510 real changes made)

{com}. replace Value = 1 if _merge==1;
{txt}(1,510 real changes made)

{com}. drop _merge;
{txt}
{com}. /* Merge country code */
> 
> merge m:1 counterpart using Countries,
>         keepusing(Yeuro Ynat)
>         nogen keep(master match);
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}             158
{txt}{col 9}from master{col 30}{res}             158{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}          52,547{txt}  
{col 5}{hline 41}

{com}. /* Restate from residency to nationality */
> 
> replace amount = amount*Value;
{txt}(48,321 real changes made)

{com}. drop Value;
{txt}
{com}. /* Construct dummy for domestic currency */
> 
> gen byte Idomestic = currency==counterpart | (currency=="EUR" & year>=Yeuro) | type>=3;
{txt}
{com}. drop currency Yeuro;
{txt}
{com}. /* Assign countries outside sample to other countries */
> 
> replace counterpart = "_OC" if year<Ynat;
{txt}(13,485 real changes made)

{com}. drop Ynat;
{txt}
{com}. /* Aggregate by nationality */
> 
> collapse (sum) amount, by(year country counterpart type Idomestic) fast;
{res}{txt}
{com}. /* Append IMF CPIS data */
> 
> append using `IMF_CPIS';
{txt}(label {bf:{txt}type_label} already defined)

{com}. /* Step 3: Construct own holdings */
> 
> /* Append amount outstanding */
> 
> append using Data2, gen(append);
{res}{txt}(label {bf:{txt}type_label} already defined)

{com}. replace country = counterpart if append;
{txt}(3,654 real changes made)

{com}. drop append;
{txt}
{com}. /* Aggregate amount outstanding */
> 
> collapse (sum) amount outstand, by(year country counterpart type Idomestic) fast;
{res}{txt}
{com}. /* Construct own holdings */
> 
> egen Tamount = total(amount*(country!=counterpart)), missing by(year counterpart type Idomestic);
{txt}
{com}. replace amount = max(amount,outstand-Tamount,$small*Idomestic) if country==counterpart;
{txt}(3,405 real changes made)

{com}. drop outstand Tamount;
{txt}
{com}. /* Drop fund shares */
> 
> drop if type==4;
{txt}(3,556 observations deleted)

{com}. /* Aggregate foreign currency assets with outside assets */
> 
> replace counterpart = "_OC" if !Idomestic;
{txt}(13,544 real changes made)

{com}. collapse (sum) amount, by(year country counterpart type) fast;
{res}{txt}
{com}. /* Step 4: Aggregate investor countries */
> 
> /* Merge country code */
> 
> merge m:1 country using Countries,
>         keepusing(Ynat)
>         nogen keep(master match);
{res}{txt}(label {bf:{txt}MSCI_label} already defined)
(label {bf:{txt}Region_label} already defined)

{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}           1,956
{txt}{col 9}from master{col 30}{res}           1,956{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}         124,734{txt}  
{col 5}{hline 41}

{com}. /* Assign countries outside sample to other countries */
> 
> replace country = "_OC" if year<Ynat & country!="_CR";
{txt}(58,877 real changes made)

{com}. drop Ynat;
{txt}
{com}. /* Aggregate across other countries */
> 
> collapse (sum) amount, by(year country counterpart type) fast;
{res}{txt}
{com}. /* Round up to minimum value for CPIS */
> 
> replace amount = max(amount,1e-6) if counterpart!="_OC";
{txt}(969 real changes made)

{com}. /* Winsorize left tail of outside assets */
> 
> egen wealthA = total(amount), missing by(year country type);
{txt}
{com}. replace amount = max(amount,1e-3*wealthA) if counterpart=="_OC";
{txt}(116 real changes made)

{com}. drop wealthA;
{txt}
{com}. /* Label variables */
> 
> order year country counterpart type;
{txt}
{com}. label var amount "Investment amount (US$ billion)";
{txt}
{com}.  /* Save data */
> 
> sort year country counterpart type;
{txt}
{com}. save Data3, replace;
{txt}{p 0 4 2}
file {bf}
Data3.dta{rm}
saved
{p_end}

{com}. log close;
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\myogo\Princeton Dropbox\Motohiro Yogo\Koijen Yogo - International\JPE\Replication\Code\1 Data\Data3.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}31 Oct 2025, 09:19:47
{txt}{.-}
{smcl}
{txt}{sf}{ul off}